// pages/payment-details/payment-details.js
Page({
  data: {
    paymentInfo: {
      feeType: '西药费',
      department: '消化内科',
      doctor: '李梅',
      patient: '牛韦',
      cardNumber: '610*********0989'
    },
    feeItems: [
      {
        id: 1,
        name: '普罗布考片',
        unitPrice: '50.00',
        quantity: 3,
        total: '150.00'
      },
      {
        id: 2,
        name: '阿司匹林肠溶片',
        unitPrice: '50.00',
        quantity: 3,
        total: '150.00'
      }
    ],
    paymentMethod: '余额',
    cardBalance: '1400.00',
    totalAmount: '300.00',
    orderNumber: '202010090900001',
    orderTime: '2020/10/09 09:00:00',
    orderStatus: '已支付'
  },

  onLoad(options) {
    console.log('缴费详情页面加载');
    
    // 接收传递的缴费信息
    if (options.paymentInfo) {
      const paymentInfo = JSON.parse(options.paymentInfo);
      
      // 计算扣减后的余额
      const currentBalance = 4100.00; // 当前余额
      const paymentAmount = parseFloat(paymentInfo.totalAmount);
      const newBalance = currentBalance - paymentAmount;
      
      // 生成订单编号
      const now = new Date();
      const orderNumber = this.generateOrderNumber(now);
      const orderTime = this.formatDateTime(now);
      
      this.setData({
        paymentInfo: {
          feeType: paymentInfo.feeType,
          department: paymentInfo.department,
          doctor: paymentInfo.doctor,
          patient: paymentInfo.patient,
          cardNumber: paymentInfo.cardNumber
        },
        feeItems: paymentInfo.feeItems,
        totalAmount: paymentInfo.totalAmount,
        cardBalance: newBalance.toFixed(2),
        orderNumber: orderNumber,
        orderTime: orderTime
      });
    } else {
      // 生成订单编号
      const now = new Date();
      const orderNumber = this.generateOrderNumber(now);
      const orderTime = this.formatDateTime(now);
      
      this.setData({
        orderNumber: orderNumber,
        orderTime: orderTime
      });
    }
  },

  // 生成订单编号
  generateOrderNumber(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    const random = String(Math.floor(Math.random() * 1000)).padStart(3, '0');
    
    return `${year}${month}${day}${hours}${minutes}${seconds}${random}`;
  },

  // 格式化日期时间
  formatDateTime(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    
    return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
  }
});
